﻿using JyUserFeedback;
using JyUserFeedback.view;
using JyUserInfo;
using JyUserInfo.Model;
using System;
using Windows.Storage.Pickers;
using Windows.UI.Popups;
using Windows.UI.Xaml;

namespace JyFeedbackDemo.Windows10
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage
    {
        private readonly JyUserFeedbackSDKManager _jyUserFeedbackSdkManager = new JyUserFeedbackSDKManager();

        private UserInfo _userInfo;

        public MainPage()
        {
            InitializeComponent();

            JyUserInfoManager.LogincompletedEvent += JyUserInfoManager_LoginCompleted;

            JyFeedbackControl.FeedbackImageRequested += async delegate
            {
                var fileOpenPicker = new FileOpenPicker();
                fileOpenPicker.FileTypeFilter.Add(".png");
                fileOpenPicker.FileTypeFilter.Add(".jpg");

                var file = await fileOpenPicker.PickSingleFileAsync();
                if (file != null)
                {
                    _jyUserFeedbackSdkManager.UploadPicture(Constants.DemoAppkey, Constants.DemoSecretId, file);
                }
            };
        }

        private async void FastLoginButton_Click(object sender, RoutedEventArgs e)
        {
            var userInfo = await JyUserInfoManager.QuickLogin(Constants.DemoAppkey);
            if (userInfo.isLoginSuccess)
            {
                _userInfo = userInfo;
                await new MessageDialog(Constants.FastLoginSuccessMessage).ShowAsync();
            }
            else
            {
                await new MessageDialog(Constants.FastLoginFailedMessage).ShowAsync();
            }
        }

        private async void GetDeveloperReplyCountButton_Click(object sender, RoutedEventArgs e)
        {
            if (_userInfo == null)
            {
                await new MessageDialog(Constants.PleaseLoginFirstMessage).ShowAsync();
            }
            else
            {
                var newFeedBackRemindCount = await _jyUserFeedbackSdkManager.GetNewFeedBackRemindCount(Constants.DemoAppkey, _userInfo.U_Key);
                await new MessageDialog(string.Format(Constants.NewFeedbackReplyCountMessage, newFeedBackRemindCount)).ShowAsync();
            }
        }

        private void JyUserInfoManager_LoginCompleted(object sender, UserInfo userInfo)
        {
            if (userInfo.isLoginSuccess)
            {
                _userInfo = userInfo;
            }
            else
            {
                // 不需要在 else 逻辑中弹出对话框显示登录失败，登录失败时，登录控件会有弹窗提示。
            }
        }

        private void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            JyUserInfoManager.ShowLogin(RootGrid, Constants.DemoAppkey, LoginTemplate.Template3);
        }

        private async void OpenFeedbackWindowButton_Click(object sender, RoutedEventArgs e)
        {
            if (_userInfo == null)
            {
                await new MessageDialog(Constants.PleaseLoginFirstMessage).ShowAsync();
            }
            else
            {
                _jyUserFeedbackSdkManager.ShowFeedBack(RootGrid, false, Constants.DemoAppkey, _userInfo.U_Key);
            }
        }
    }
}